package util.preprocessing;

import java.util.Enumeration;
import java.util.Hashtable;

import util.io.FileInput;

public class StatsHelper {

	/**
	 * @param args
	 */

	public static void query_clicks_Distro(String queries, String clicks) {

		Hashtable<String, Integer> q = new Hashtable<String, Integer>();
		Hashtable<String, Integer> c = new Hashtable<String, Integer>();

		fillHash(queries, q);
		fillHash(clicks, c);

		Hashtable<Integer, Cell> stat = new Hashtable<Integer, Cell>();

		Enumeration<String> iter = q.keys();

		while (iter.hasMoreElements()) {

			String key = iter.nextElement();

			Integer temp = q.get(key);
		
			if (stat.containsKey(temp) && !key.equals("null")) {

				Cell t = stat.get(temp);
				t.items++;
				
			
				t.count += c.get(key);
				stat.put(temp, t);
	
			} else {
				if(!key.equals("null")){
				Cell t = new Cell();
				t.items++;
				t.count = c.get(key);

				stat.put(temp, t);
				}
			}

		}

		Enumeration<Integer> iter2 = stat.keys();

		while (iter2.hasMoreElements()) {

			Integer i = iter2.nextElement();

			System.out
					.println(i
							+ "\t"
							+ (float) ((float) stat.get(i).count / (float) stat
									.get(i).items));

		}

	}

	public static void fillHash(String path, Hashtable<String, Integer> hash) {

		FileInput in = new FileInput(path);

		String line = in.readString();
		while (line != null) {
		
			String t[] = line.split("\t");
			if(t.length>1){
			
			hash.put(t[1], Integer.valueOf(t[0]));
			}
			line = in.readString();
			

		}
	}
	
	
	public static void fillHash2(String path, Hashtable<String, Integer> hash) {

		FileInput in = new FileInput(path);

		String line = in.readString();
		while (line != null) {
		
			String t[] = line.split("\t");
			if(t.length>1){
			
			hash.put(t[1], Integer.valueOf(t[0]));
			}
			line = in.readString();
			

		}
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		String q = "/project/CODFC/queryAnalysis/helperQueryClicks_all.txt"; //host counts
		String c = "/project/CODFC/queryAnalysis/helperQueryClicks_uniqueQueries_all.txt";//
		
		query_clicks_Distro(q,c);
		
	}

}

class Cell {
int items=0;
int count=0;
}
